package com.archie.zhufengfm.fragments.home.discover;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;

import com.archie.zhufengfm.AlbumDetailActivity;
import com.archie.zhufengfm.Constants;
import com.archie.zhufengfm.R;
import com.archie.zhufengfm.adapters.DiscoverRecommendAdapter;
import com.archie.zhufengfm.adapters.PicPagerAdapter;
import com.archie.zhufengfm.entity.discoverrecommend.AlbumRecommend;
import com.archie.zhufengfm.entity.discoverrecommend.ColumnsItem;
import com.archie.zhufengfm.entity.discoverrecommend.DiscoverRecommendItem;
import com.archie.zhufengfm.entity.discoverrecommend.SpecialItem;
import com.archie.zhufengfm.fragments.home.BaseFragment;
import com.archie.zhufengfm.tasks.DiscoverRecommendTask;
import com.archie.zhufengfm.tasks.TaskCallback;
import com.archie.zhufengfm.tasks.TaskResult;
import com.archie.zhufengfm.utils.DimensionUtil;

import org.json.JSONObject;

import java.util.LinkedList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class DiscoverRecommendFragment extends BaseFragment implements TaskCallback, View.OnClickListener {

    private DiscoverRecommendAdapter adapter;
    private List<DiscoverRecommendItem> items;

    // 轮播海报
    private ViewPager focusImagesPager;

    public DiscoverRecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public String getFragmentTitle() {

        return "推荐";
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        items = new LinkedList<>();

        adapter = new DiscoverRecommendAdapter(getActivity(), items);

    }

    // 因为每次进入推荐的时候，都会触发内容的刷新
    @Override
    public void onResume() {
        super.onResume();

        DiscoverRecommendTask task = new DiscoverRecommendTask(this);
        task.execute();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View ret = inflater.inflate(R.layout.fragment_discover_recommend, container, false);

        ListView listView = (ListView) ret.findViewById(R.id.discover_recommend_list);

        // 创建 ViewPager
        focusImagesPager = new ViewPager(getActivity());

        AbsListView.LayoutParams lp =
                new AbsListView.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT,
                        DimensionUtil.dp2px(getActivity(), 60)
                );

        focusImagesPager.setLayoutParams(lp);

        LinkedList picData = new LinkedList();

        //假数据
        picData.add("Stirng");

        PicPagerAdapter picAdapter = new PicPagerAdapter(picData);
        focusImagesPager.setAdapter(picAdapter);

        listView.addHeaderView(focusImagesPager);

        // ----------------------------------------------------

        // 设置专辑推荐点击事件
        adapter.setOnRecommendAlbumClickListener(this);

        // ----------------------------------------------------

        listView.setAdapter(this.adapter);

        focusImagesPager.setCurrentItem(Integer.MAX_VALUE >> 1);

        return ret;
    }

    @Override
    public void onTaskFinished(TaskResult result) {

        // TODO 处理推荐列表的数据
        if (result != null) {

            if(result.action == Constants.TASK_ACTION_DISCOVER_RECOMMENDS){

                if(result.resultCode == Constants.TASK_RESULT_OK){

                    Object data = result.data;

                    if (data != null) {
                        if (data instanceof List) {

                            List list = (List) data;

                            // TODO　更新ListView 的 Adapter
                            // 只要数据来了就清除 adapter
                            items.clear();

                            for(Object o : list){

                                if(o instanceof DiscoverRecommendItem){

                                    items.add((DiscoverRecommendItem)o);

                                }

                            }

                            adapter.notifyDataSetChanged();

                        }else if(data instanceof JSONObject){



                        }
                    }

                }
            }
        }

    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag();

        if (tag != null) {

            if(tag instanceof AlbumRecommend) {

                AlbumRecommend albumRecommend = (AlbumRecommend) tag;

                // 专辑 ID
                long albumId = albumRecommend.getAlbumId();

                // 曲目 ID
                long trackId = albumRecommend.getTrackId();

                Intent intent = new Intent(getActivity(), AlbumDetailActivity.class);

                intent.putExtra("albumId", albumId);

                intent.putExtra("trackId", trackId);

                startActivity(intent);

            }else if(tag instanceof SpecialItem){

                SpecialItem specialRecommend = (SpecialItem) tag;

                // 精品 ID
                long specialId = specialRecommend.getSpecialId();

                Toast.makeText(getActivity(), "点击了：" + specialId, Toast.LENGTH_SHORT).show();

            }else if(tag instanceof ColumnsItem){

                ColumnsItem ColumnsRecommend = (ColumnsItem) tag;

                // 发现新奇 ID
                long id = ColumnsRecommend.getId();

                Toast.makeText(getActivity(), "点击了：" + id, Toast.LENGTH_SHORT).show();

            }

        }

    }
}
